package com.zut.dao;

import com.sun.org.apache.xpath.internal.operations.Lt;
import com.zut.domain.Post;
import com.zut.domain.Reply;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ReplyDao {

    @Results(id = "groupWithReply",
            value = {
                    @Result(property = "replyId", column = "reply_id", id = true),
                    @Result(property = "postId", column = "post_id"),
                    @Result(property = "userId", column = "reply_user_id"),
                    @Result(property = "content", column = "reply_content"),
                    @Result(property = "date", column = "reply_date"),
                    @Result(property = "like", column = "reply_like"),
                    @Result(property = "type", column = "reply_user_type")
            })
    @Select("select * from reply")
    List<Reply> getAllPost();

    @Insert("insert into reply (post_id,reply_user_id,reply_content,reply_date,reply_like,reply_user_type) " +
            "values(#{postId},#{userId},#{content},#{date},#{like},#{type})")
    int insert(Reply reply);

    @Delete("delete from reply where reply_id = #{replyId}")
    int deleteById(String id);

    @Update("<script>                                                               "
            + "update reply                                                         "
            + "<set>                                                                "
            + "  <if test='content != null'> reply_content=#{content},    </if>     "
            + "  <if test='date != null'> reply_date=#{date},    </if>              "
            + "</set>                                                               "
            + "where post_id = #{postId} and reply_id = #{replyId}                  "
            + "</script>                                                            ")
    int update(Reply reply);

    @Update("update reply set reply_like = reply_like + 1 where reply_id = #{replyId}")
    int updateLikeOn(String replyId);

    @Update("update reply set reply_like = reply_like - 1 where reply_id = #{replyId}")
    int updateLikeDown(String replyId);

    @Select("select distinct reply.reply_id as replyId," +
            "reply_user_id as userId," +
            "if(reply_user_type = '学员',user_name,coach_name) as name," +
            "if(reply_user_type = '学员',user_sex,coach_sex) as sex," +
            "if(reply_user_type = '学员',user_img,coach_img) as img," +
            "if(fond.user_id is null,'0','1') as status," +
            "reply_content as content,reply_like as likeCount," +
            "reply_date as date,reply_user_type as type " +
            "from reply " +
            "left join student on reply_user_id = student.user_id " +
            "left join coach on reply_user_id = coach.coach_id " +
            "left join fond on reply.reply_id = fond.reply_id and fond.user_id = #{userId} " +
            "where post_id = #{postId} order by reply_date asc")
    List<Map<String, String>> getAll(String postId, String userId);

    @Select("select reply_id as replyId," +
            "reply.post_id as postId," +
            "if(post_user_type = '学员',user_name,coach_name) as postUserName," +
            "if(post_user_type = '学员',student.user_id,coach.coach_id) as postUserId," +
            "post_user_type as postUserType," +
            "reply_content as content," +
            "reply_date as date,reply_like as likeNum from reply " +
            "left join post on reply.post_id = post.post_id " +
            "left join student on post.user_id = student.user_id " +
            "left join coach on post.user_id = coach.coach_id " +
            "where reply_user_id = #{id}")
    List<Map<String, String>> getAllByUserID(String id);

    @ResultMap("groupWithReply")
    @Select("select * from reply where post_id = #{id}")
    List<Reply> getAllByPostId(String id);

}
